{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Load Spatial Data Frame Tutorial\n",
    "\n",
    "This is the completed sample for the [Load spatial data frame](https://developers.arcgis.com/labs/python/load-spatial-data-frame/) ArcGIS tutorial. \n",
    "\n",
    "[ArcGIS tutorials](https://developers.arcgis.com/labs/) are short guides demonstrating the three phases of building geospatial apps: Data, Design, Develop.\n",
    "\n",
    "__Note__: Please complete the [Import Data](https://developers.arcgis.com/labs/python/import-data/) tutorial if you have not done so already. You will use the output feature layer from this lab to learn how to find and share an item."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "from arcgis.gis import GIS\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Log into ArcGIS Online by making a GIS connection using your developer account. Replace `username` and `password` with your own credentials."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Enter password, please: ········\n"
     ]
    }
   ],
   "source": [
    "import getpass\n",
    "\n",
    "password = getpass.getpass(\"Enter password, please: \")\n",
    "gis = GIS('https://arcgis.com', 'username', password)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Search for the *Trailheads* layer used in the [Import Data](https://developers.arcgis.com/labs/python/import-data/) tutorial."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[<Item title:\"Trailheads\" type:Feature Layer Collection owner:cff_dev>]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "feature_service_srch_results = gis.content.search(query='title: \"Trailheads*\" AND type: \"Feature Service\"')\n",
    "feature_service_srch_results"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Retrieve the feature service item from the list of results. Then, get the layer from that service."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<FeatureLayer url:\"https://services9.arcgis.com/ZPYuLYCJmpcXcod0/arcgis/rest/services/Trailheads/FeatureServer/0\">"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "feature_service_item = feature_service_srch_results[0]\n",
    "feature_layer = feature_service_item.layers[0]\n",
    "feature_layer"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Build the Spatial Pandas Dataframe"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>CITY_JUR</th>\n",
       "      <th>ELEV_FT</th>\n",
       "      <th>FEATURE</th>\n",
       "      <th>FEAT_CMNT</th>\n",
       "      <th>FID</th>\n",
       "      <th>LAT</th>\n",
       "      <th>LON</th>\n",
       "      <th>OBJECTID</th>\n",
       "      <th>ObjectId2</th>\n",
       "      <th>PARKING</th>\n",
       "      <th>...</th>\n",
       "      <th>PHOTO</th>\n",
       "      <th>SEG_ID</th>\n",
       "      <th>SHAPE</th>\n",
       "      <th>SUP_DIST</th>\n",
       "      <th>TRL_ID</th>\n",
       "      <th>TRL_NAME</th>\n",
       "      <th>X_STREET</th>\n",
       "      <th>ZIP_CODE</th>\n",
       "      <th>x</th>\n",
       "      <th>y</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>Unincorporated Los Angeles County</td>\n",
       "      <td>831</td>\n",
       "      <td>TAPO</td>\n",
       "      <td>Auto Generated and Verified</td>\n",
       "      <td>1</td>\n",
       "      <td>34.113057</td>\n",
       "      <td>-118.751527</td>\n",
       "      <td>7769</td>\n",
       "      <td>1</td>\n",
       "      <td>SA</td>\n",
       "      <td>...</td>\n",
       "      <td>None</td>\n",
       "      <td>200820000</td>\n",
       "      <td>{\"x\": -13219359.478715051, \"y\": 4043992.964497...</td>\n",
       "      <td>3rd District</td>\n",
       "      <td>20082.0</td>\n",
       "      <td>Medea Creek Trail</td>\n",
       "      <td>28825 Lake Vista Dr</td>\n",
       "      <td>91301</td>\n",
       "      <td>-118.751527</td>\n",
       "      <td>34.113057</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>Unincorporated Los Angeles County</td>\n",
       "      <td>1555</td>\n",
       "      <td>TAPO</td>\n",
       "      <td>None</td>\n",
       "      <td>2</td>\n",
       "      <td>34.059687</td>\n",
       "      <td>-118.616545</td>\n",
       "      <td>8047</td>\n",
       "      <td>2</td>\n",
       "      <td>ST</td>\n",
       "      <td>...</td>\n",
       "      <td>None</td>\n",
       "      <td>200670000</td>\n",
       "      <td>{\"x\": -13204333.421340073, \"y\": 4036819.355827...</td>\n",
       "      <td>3rd District</td>\n",
       "      <td>20067.0</td>\n",
       "      <td>Hearst Tank Motorway</td>\n",
       "      <td>3200 Tuna Canyon Rd</td>\n",
       "      <td>90290</td>\n",
       "      <td>-118.616545</td>\n",
       "      <td>34.059687</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>Unincorporated Los Angeles County</td>\n",
       "      <td>758</td>\n",
       "      <td>TH</td>\n",
       "      <td>Auto Generated and Verified</td>\n",
       "      <td>3</td>\n",
       "      <td>34.117364</td>\n",
       "      <td>-118.756839</td>\n",
       "      <td>7770</td>\n",
       "      <td>3</td>\n",
       "      <td>SA</td>\n",
       "      <td>...</td>\n",
       "      <td>None</td>\n",
       "      <td>200840000</td>\n",
       "      <td>{\"x\": -13219950.875755034, \"y\": 4044572.018172...</td>\n",
       "      <td>3rd District</td>\n",
       "      <td>20084.0</td>\n",
       "      <td>Medicine Woman Trail</td>\n",
       "      <td>2900 Cornell Rd</td>\n",
       "      <td>91301</td>\n",
       "      <td>-118.756839</td>\n",
       "      <td>34.117364</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>Los Angeles</td>\n",
       "      <td>0</td>\n",
       "      <td>TAPO</td>\n",
       "      <td>Auto Generated and Verified</td>\n",
       "      <td>4</td>\n",
       "      <td>34.079346</td>\n",
       "      <td>-118.519724</td>\n",
       "      <td>8048</td>\n",
       "      <td>4</td>\n",
       "      <td>ST</td>\n",
       "      <td>...</td>\n",
       "      <td>None</td>\n",
       "      <td>101330000</td>\n",
       "      <td>{\"x\": -13193555.284564307, \"y\": 4039461.172009...</td>\n",
       "      <td>3rd District</td>\n",
       "      <td>10133.0</td>\n",
       "      <td>Pipe Corral Spur Trail</td>\n",
       "      <td>None</td>\n",
       "      <td>90272</td>\n",
       "      <td>-118.519724</td>\n",
       "      <td>34.079346</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>Unincorporated Los Angeles County</td>\n",
       "      <td>1982</td>\n",
       "      <td>TH</td>\n",
       "      <td>Auto Generated and Verified</td>\n",
       "      <td>5</td>\n",
       "      <td>34.079060</td>\n",
       "      <td>-118.755563</td>\n",
       "      <td>7773</td>\n",
       "      <td>5</td>\n",
       "      <td>ST</td>\n",
       "      <td>...</td>\n",
       "      <td>None</td>\n",
       "      <td>101120000</td>\n",
       "      <td>{\"x\": -13219808.808707688, \"y\": 4039422.795144...</td>\n",
       "      <td>3rd District</td>\n",
       "      <td>10112.0</td>\n",
       "      <td>Mesa Peak Motorway</td>\n",
       "      <td>300 Corral Canyon Rd</td>\n",
       "      <td>90265</td>\n",
       "      <td>-118.755563</td>\n",
       "      <td>34.079060</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 21 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                            CITY_JUR ELEV_FT FEATURE  \\\n",
       "0  Unincorporated Los Angeles County     831    TAPO   \n",
       "1  Unincorporated Los Angeles County    1555    TAPO   \n",
       "2  Unincorporated Los Angeles County     758      TH   \n",
       "3                        Los Angeles       0    TAPO   \n",
       "4  Unincorporated Los Angeles County    1982      TH   \n",
       "\n",
       "                     FEAT_CMNT FID        LAT         LON OBJECTID  ObjectId2  \\\n",
       "0  Auto Generated and Verified   1  34.113057 -118.751527     7769          1   \n",
       "1                         None   2  34.059687 -118.616545     8047          2   \n",
       "2  Auto Generated and Verified   3  34.117364 -118.756839     7770          3   \n",
       "3  Auto Generated and Verified   4  34.079346 -118.519724     8048          4   \n",
       "4  Auto Generated and Verified   5  34.079060 -118.755563     7773          5   \n",
       "\n",
       "  PARKING  ... PHOTO     SEG_ID  \\\n",
       "0      SA  ...  None  200820000   \n",
       "1      ST  ...  None  200670000   \n",
       "2      SA  ...  None  200840000   \n",
       "3      ST  ...  None  101330000   \n",
       "4      ST  ...  None  101120000   \n",
       "\n",
       "                                               SHAPE      SUP_DIST   TRL_ID  \\\n",
       "0  {\"x\": -13219359.478715051, \"y\": 4043992.964497...  3rd District  20082.0   \n",
       "1  {\"x\": -13204333.421340073, \"y\": 4036819.355827...  3rd District  20067.0   \n",
       "2  {\"x\": -13219950.875755034, \"y\": 4044572.018172...  3rd District  20084.0   \n",
       "3  {\"x\": -13193555.284564307, \"y\": 4039461.172009...  3rd District  10133.0   \n",
       "4  {\"x\": -13219808.808707688, \"y\": 4039422.795144...  3rd District  10112.0   \n",
       "\n",
       "                 TRL_NAME              X_STREET ZIP_CODE           x  \\\n",
       "0       Medea Creek Trail   28825 Lake Vista Dr    91301 -118.751527   \n",
       "1    Hearst Tank Motorway   3200 Tuna Canyon Rd    90290 -118.616545   \n",
       "2    Medicine Woman Trail       2900 Cornell Rd    91301 -118.756839   \n",
       "3  Pipe Corral Spur Trail                  None    90272 -118.519724   \n",
       "4      Mesa Peak Motorway  300 Corral Canyon Rd    90265 -118.755563   \n",
       "\n",
       "           y  \n",
       "0  34.113057  \n",
       "1  34.059687  \n",
       "2  34.117364  \n",
       "3  34.079346  \n",
       "4  34.079060  \n",
       "\n",
       "[5 rows x 21 columns]"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sdf = pd.DataFrame.spatial.from_layer(feature_layer)\n",
    "sdf.head()"
   ]
  }
 ],
 "metadata": {
  "esriNotebookRuntime": {
   "notebookRuntimeName": "ArcGIS Notebook Python 3 Standard",
   "notebookRuntimeVersion": "9.0"
  },
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
